package org.dubbo.consumer;

import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.common.extension.Adaptive;
import com.alibaba.dubbo.common.extension.ExtensionFactory;
import com.alibaba.dubbo.common.extension.ExtensionLoader;
import com.alibaba.dubbo.common.extension.SPI;

import java.util.HashMap;
import java.util.Map;

public class Test {

    public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException {
        Test test = new Test();
        test.t();
    }

    public void t() throws NoSuchMethodException, SecurityException, IllegalAccessException, InstantiationException {


        Map<String, String> parameters = new HashMap<String, String>();
        parameters.put("gen", "gen");
        URL url = new URL("127.0.0.1", "zookeepr", 80, parameters);
        Class<Gen> genLoaderClass = null;
        try {
            genLoaderClass = (Class<Gen>) Class.forName("org.dubbo.consumer.Gen$Adpative");

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
//        RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
//        String name = runtime.getName(); // format: "pid@hostname"
        // int PID = Integer.parseInt(name.substring(0, name.indexOf('@')));
        // System.out.println(PID);
        // System.out.println(InvocationTargetException.class.getName());
        //
        // System.out.println(Test.class.getMethod("toString",
        // null).getDeclaringClass());
        // System.out.println(getClass().getSimpleName());
//
//        ExtensionLoader<ExtensionFactory> loader = ExtensionLoader.getExtensionLoader(ExtensionFactory.class);
//        ExtensionFactory ef = loader.getAdaptiveExtension();
//        ExtensionFactory ef2 = loader.getExtension("adaptive");
//        System.out.println(ef == ef2);
//
//		Gen genImpl = 	ef.getExtension(Gen.class,"fk");
//
//		Map<String, String> parameters = new HashMap<String, String>();
//		parameters.put("gen", "gen");
//		URL url = new URL("127.0.0.1", "zookeepr", 80, parameters);
//
//		genImpl.say(url);

//        ExtensionLoader<Gen> genLoader = ExtensionLoader.getExtensionLoader(Gen.class);
//
//        System.out.println("is true" + genLoaderClass.getClassLoader() + "xxx" + genLoader.getAdaptiveExtension().getClass().getClassLoader());
           genLoaderClass.newInstance().say(url);



        org.dubbo.consumer.Gen extension = (org.dubbo.consumer.Gen) ExtensionLoader
                .getExtensionLoader(org.dubbo.consumer.Gen.class).getAdaptiveExtension();
        extension.say(url);

//        Gen$Adpative ga = new Gen$Adpative();
//        ga.say(url);
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return super.toString();
    }

}

@SPI
interface Gen {
    @Adaptive(value = "gen")
    void say(URL url);

}